package org.ruanjj.multithread.JUC;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

//ArrayList 线程安全问题
public class ArrayListDemo {
    /**
     * 1.故障现象：java.util.ConcurrentModificationException
     *
     * 2.导致的原因
     *
     *
     * 3.解决方案
     * 3.1.new Vector()
     * 3.2.Collections.synchronizedList(new ArrayList<>());
     *
     *
     * **/
    public static void main(String[] args) throws InterruptedException {
        List<String> list = Collections.synchronizedList(new ArrayList<>());
        CopyOnWriteArraySet<String> set = new CopyOnWriteArraySet<>();
        set.add("test");

        for (int i = 0; i < 30; i++) {
            new Thread(()->{
                for (int j = 0; j < 10; j++) {
                    list.add("test");
                    System.out.println(list);
                }
            }, String.valueOf(i)).start();
        }
    }
}
